﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace BetResult
{
    public static class BetXml
    {
        public static string GetCurLogFile()
        {
            DateTime dt = DateTime.Now;
            //return "c:\\" + dt.Year.ToString() + dt.Month.ToString().PadLeft(2, '0') + dt.Day.ToString().PadLeft(2, '0') + ".xml";
            return "c:\\result.xml";
        }

        public static bool WriteResult(string peroid, string result)
        {
            string file = GetCurLogFile();
            XmlDocument doc = new XmlDocument();
            if (!File.Exists(file))
            {
                XmlDeclaration declare = doc.CreateXmlDeclaration("1.0", "gb2312", null);
                doc.AppendChild(declare);
                XmlElement ele = doc.CreateElement("BetResults");
                doc.AppendChild(ele);
                doc.Save(file);
            }
            else
            {
                doc.Load(file);
            }

            XmlNode root = doc.SelectSingleNode("BetResults");
            if (root == null)
                return false;

            XmlNodeList list = root.SelectNodes("BetResult");
            foreach (XmlNode node in list)
            {

                XmlNode myNode = node.SelectSingleNode("Peroid");
                if (myNode != null)
                {
                    if (myNode.InnerText == peroid)
                    {
                        return false;
                    }
                }

            }
            XmlElement newele = doc.CreateElement("BetResult");
            XmlElement sub = doc.CreateElement("Peroid");
            sub.InnerText = peroid;
            newele.AppendChild(sub);
            sub = doc.CreateElement("Result");
            sub.InnerText = result;
            newele.AppendChild(sub);
            root.AppendChild(newele);
            doc.Save(file);
            return true;
        }



        public static void CalulateResult(ref string bet2, ref string bet3, ref string total)
        {
            string file = GetCurLogFile();
            XmlDocument doc = new XmlDocument();
            int bet2Count = 0;
            int bet3Count = 0;
            float getTotal = 0F;
            if (!File.Exists(file))
            {
                bet2 = "0";
                bet3 = "0";
                total = "0";
                return;
            }
            else
            {
                doc.Load(file);
                XmlNode root = doc.SelectSingleNode("BetHistorys");
                foreach(XmlNode node in root.ChildNodes)
                {
                    XmlNode nd = null;
                    nd = node.SelectSingleNode("押注类型");
                    if (nd == null)
                        continue;
                    switch (nd.InnerText)
                    {
                        case "2":
                            bet2Count++;
                        break;
                        case "3":
                            bet3Count++;
                        break;
                    }
                    nd = node.SelectSingleNode("当前盈利");
                    if (nd == null)
                        continue;

                    float value = 0F;
                    Single.TryParse(nd.InnerText, out value);
                    getTotal = getTotal + value;
                }

                bet2 = bet2Count.ToString();
                bet3 = bet3Count.ToString();
                total = getTotal.ToString();
            }

        }




        public static XmlNodeList GetNodeList(string file)
        {
            if (!File.Exists(file))
                return null;

            XmlDocument doc = new XmlDocument();
            doc.Load(file);
            XmlNode root = doc.SelectSingleNode("BetHistorys");
            if (root == null)
                return null;

            return root.ChildNodes;
        }

    }
}
